'''
Created on 2009-11-20

@author: yuchen
'''
import gamephase.AbstractCheckerGameMode as mBase
import gamemodel.state as mState
import gamemodel.player as mPlayer
import ai.computerHeuri as mAI
import ai.computerMento as nAI
import interface.ConsoleAgent as itfc
import global_controller

class ComputerVsComputer(mBase.AbstractCheckerGameMode):
	'''
	computer player vs. computer player
	'''

	def __init__(self):
		self.state = None
		self.status = ""
	

	def execute(self):
		if self.status == "" :
			self.CreateGame()
			self.status = "Run" 
		elif self.status == "Run" :
			self.Tick()
		elif self.status == "End":
			self.end_game()
		pass
	
	def CreateGame(self):
		player0 = mPlayer.CheckerPlayer("DeepBlue")
		player0.attachAI(mAI.AIcomputer)
		player1 = mPlayer.CheckerPlayer("Chinook")
		player1.attachAI(nAI.AIcomputer)
		state = mState.CheckerState.constructNew(player0, player1)
		self.state = state
	
	def getGameState(self):
		return self.state
	
	def setGameState(self, state):
		self.state = state
	
	def Tick(self):
		print
		itfc.ConsoleDescriptor.drawState(self.state)
		
		player = self.state.getCurrentPlayer()
		move = player.getNextMove(self.state)
		if move == None :
			print "Ah,", player," loses!!!"
			print "Unbelievable!!!!!"
			self.status = "end"
			return
		
		if move == 'draw' :
			print "Oh, it is draw..."
			self.status = "end"
			return
		
		print "The move for the player ",self.state.getCurrentPlayer(),"is :"
		print move.list[0].original, "to", move.list[0].destination
		print "Change player"
		print
		
		# proceed state, switch player
		self.take_move(move)
		self.state.round = self.state.round + 0.5
		print "Now round is", self.state.round	
		
		#delay some time, so user can have a look
		global_controller.GC.delay(2)
		return